#include <stdint.h>
#include "stm32f4xx.h"
#include "delay.h"

void delay_us(uint32_t us)
{
    SysTick->LOAD = us * (SystemCoreClock / 1000000) - 1; // Load the SysTick counter value
    SysTick->VAL = 0; // Clear the current value of the SysTick counter
    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; // Start the SysTick timer
    while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)); // Wait until the count flag is set
    SysTick->CTRL = 0; // Stop the SysTick timer
}

void delay_ms(uint32_t ms)
{
    for (uint32_t i = 0; i < ms; i++)
    {
        delay_us(1000); // Call the microsecond delay function for 1000 microseconds (1 millisecond)
    }
}
